/**
 * 
 */
package com.qolome.utils.web;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description: Http工具类
 * @author 姜锋
 * @date 2018年12月21日 下午2:30:14 
 * @version V1.0   
 *
 */
public class HttpClientUtils {
	
	/** 日志对象 **/
	private final static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
	
	/**
	 * 	底层HTTP连接仍由响应对象保持
	 * 	允许响应内容直接从网络套接字流式传输。
	 * 	为了确保正确释放系统资源
	 * 	用户必须从finally子句中调用CloseableHttpResponse #close（）。
	 * 	请注意，如果响应内容没有完全消耗，则基础
	 * 	连接无法安全重复使用，将被关闭并丢弃由连接管理器。
	 */
	
	/**
	 * @Description: 使用HttpClient本机API执行HTTP GET
	 * @author 姜锋
	 * @date 2018年12月21日 上午11:17:04
	 * @param @throws ClientProtocolException
	 * @param @throws IOException
	 * @return void
	 * @throws
	 */
	public static HttpEntity Get(String url) throws ClientProtocolException, IOException{
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = httpclient.execute(httpGet);
		try {
		return  response.getEntity();
		} finally {
			response.close();
		}
		
	}
	
	/**
	 * @Description: 使用HttpClient本机API执行HTTP POST
	 * @author 姜锋
	 * @date 2018年12月21日 上午11:17:04
	 * @param @throws ClientProtocolException
	 * @param @throws IOException
	 * @return void
	 * @throws
	 */
	public void Post (String url, List<NameValuePair> list) throws ClientProtocolException, IOException{
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.setEntity(new UrlEncodedFormEntity(list));
		CloseableHttpResponse response = httpclient.execute(httpPost);
		try {
			logger.info("输出打印结果-->  "+response.getStatusLine());
			HttpEntity entity2 = response.getEntity();
			EntityUtils.consume(entity2);
		} finally {
			response.close();
		}
	}
	
	
}
